<?php
/**
 * Kеш въюх
 * 
 * Kеш въюх
 * @author Anton Zavidov
 * @package myLiveShop
 */

/**
 * Kеш въюх
 *
 */
class Viewcache extends Base {
	private static $template_extention = '.html';
	private static $name;
	private static $script_list = array();
	/**
	 * Констуктор
	 *
	 * @param string $className имя подели
	 */
	public function __construct($className=null){

		parent::__construct($className?$className:__CLASS__);
	}
	protected static function getPath($name=false){
		if(!$name){
			$name = array(
			"lang" => Vars::_(Router::LANG),
			"shop" => Vars::_(Router::SHOP),
			"controller" => Vars::_(Router::CONTROLLER),
			);
		}
		self::$name = $name;
		$path = Config::_('path_cache_dir');
		if($name['shop']){
			$path .= 'shops'.DS.$name['shop'].DS;
		}
		self::changeView();
		return $path;
	}
	public static function _($name=false){
		$file = self::getPath($name).self::$name['lang'].DS.self::$name['controller'].Config::_('default_cache_extend');
		if(Config::_('view_cache') && File::exists($file)){
			$cache = File::_($file);
		}else{
			L('Создаётся новый кеш шаблон '.$file);
			$save_cache = Config::_('view_cache');
			$cache = File::_('/view/templates/header'.self::$template_extention);
			if(!File::exists('/view/templates/'.self::$name['controller'].self::$template_extention)){
				L('Не найдена основа шаблона '.self::$name['controller']);
				$cache .= File::_('/view/templates/404'.self::$template_extention);
				$save_cache = false;
			}else{
				$cache .= File::_('/view/templates/'.self::$name['controller'].self::$template_extention);
			}
			$cache .= File::_('/view/templates/footer'.self::$template_extention);
			$cache .= self::getScriptList();
			$cache .= File::_('/view/templates/end'.self::$template_extention);
			if($save_cache){
				if(File::_($file,$cache,array('add'=>true))){
					L('Записан новый кеш шаблон '.$file);
				}else{
					new CoreExeption('Невозможно записать кеш шаблона '.$file, E_WARNING,__FILE__,__LINE__,self::$name);
				}
			}
		}
		echo $cache;
	}
	private static function changeView($controller){
		switch (self::$name['controller']) {
			case 'category':
			case 'news':
			case 'article':
				self::$name['controller'] = 'default';
				break;
			case 'register_shop':
			case 'edit_shop':
				self::$name['controller'] = 'with_left_column';
				self::addScript('edit_shop');
				break;
			case 'manage_shop':
				self::$name['controller'] = 'with_left_column';
				self::addScript('manage_shop');
				break;
			case 'manage_article':
				self::$name['controller'] = 'with_left_column';
				self::addScript('manage_article');
				break;
			case 'create_article':
			case 'edit_article':
				self::$name['controller'] = 'with_left_column';
				self::addScript('edit_article');
				break;
			default:
				break;
		}
	}
	private static function getScriptList(){
		
		if(!count(self::$script_list)) return "";
		foreach (self::$script_list as &$item) {
			$item = '<script type="text/javascript" src="/js/'.$item.'.js"></script>';
		}
		return implode('',self::$script_list);
	}
	public static function addScript($script){
		if(!in_array($script, self::$script_list)){
			self::$script_list[] = $script;
		}
	}
}